﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
using System.Reflection;
using UMC.Web;
using UMC.Wiki.Entities;
using UMC.Web.UI;

namespace UMC.Wiki.Activities
{
    /// <summary>
    /// 邮箱账户
    /// </summary>
    class SubjectMemberActivity : WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {

            var key = this.AsyncDialog("Key", g => this.DialogValue("EDITER"));

            var  projectId = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Project", "EDITER"));
            var type = this.AsyncDialog("Type", "All");


            var userId = UMC.Data.Utility.Guid(Web.UIDialog.AsyncDialog(this.Context, "UserId", dKey =>
            {
                if (request.SendValues == null || request.SendValues.ContainsKey("start") == false)
                {
                    var buider = new UISectionBuilder(request.Model, request.Command, request.Arguments);
                    buider.CloseEvent("UI.Event");
                    this.Context.Send(buider.Builder(), true);
                }

                var webr = UMC.Web.WebResource.Instance();
                var form = request.SendValues ?? new UMC.Web.WebMeta();

                int limit = UMC.Data.Utility.IntParse(form["limit"] as string, 25);
                int start = UMC.Data.Utility.IntParse(form["start"] as string, 0);

                var sestion = start == 0 ? UISection.Create(new UIHeader().Search("搜索"), new UITitle("选择成员")) : UISection.Create();

                var search = new ProjectMember
                {
                    project_id = projectId
                };
                switch (type)
                {
                    default:
                        break;
                    case "User":
                        search.AuthType = WebAuthType.User;
                        break;
                    case "Guest":
                        search.AuthType = WebAuthType.Guest;
                        break;
                    case "Admin":
                        search.AuthType = WebAuthType.Admin;

                        break;
                }
                var Keyword = (form["Keyword"] as string ?? String.Empty);


                if (String.IsNullOrEmpty(Keyword) == false)
                {
                    search.Alias = Keyword;
                }
                int total;
                UMC.Data.Utility.Each(DataFactory.Instance().Search(search, start, out total, limit), dr =>
                 {

                     var cellUI = new UIIconNameDesc(new UIIconNameDesc.Item(webr.ImageResolve(request.Url,dr.user_id.Value, "1", "4"), dr.Alias,
                                               dr.CreationTime.ToString())
                                                       .Click(new UIClick( request, dKey, dr.user_id.ToString())));
                     sestion.Add(cellUI);

                 });
                if (sestion.Length == 0 && start == 0)
                {
                    sestion.IsNext = false;
                    sestion.Add("Desc", new UMC.Web.WebMeta().Put("desc", "未找到对应项目成员").Put("icon", "\uF016"), new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}"),
                        new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)));
                }
                else
                {
                    sestion.StartIndex = total;
                    sestion.IsNext = true;

                }



                response.Redirect(sestion);
                return this.DialogValue("none");
            }));


            var member = DataFactory.Instance().ProjectMember(projectId.Value, userId.Value);


            this.Context.UIEvent(key, new ListItem(member.Alias, member.user_id.ToString()), true);


        }
    }
}